/*
 * @Author: REFUSE_C
 * @Date: 2022-02-28 14:38:29
 * @LastEditors: tushan
 * @LastEditTime: 2022-12-18 21:15:04
 * @Description: 工具类
 */
import moment from "moment";
import merge from 'deepmerge'
/**
 * @param {string} v
 * @Description: 手机号脱敏
 */
export function formatMobile(v) {
    if (!v) return '';
    const str = v.toString();
    const reg = /(\d{3})\d{4}(\d{4})/;
    return str.replace(reg, '$1 **** $2');
}

/**
 * @param {string} v
 * @param {*} len 展示几位 默认展示一位
 * @Description: 姓名/地址脱敏
 */
export function formatName(v = '', len = 1) {
    if (!v) return '';
    return v.substr(0, len) + new Array(v.length - len + 1).join('*');
}

/**
 * @param  price 单位元
 * @param  decimal 小数位
 * @Description: 格式化价格
 */
export function formatPrice(price, decimal = 2) {
    const isNegative = price < 0;
    const realPrice = Math.abs(price).toFixed(decimal);
    const priceInt = realPrice.split('.')[0];
    const priceDecimal = realPrice.split('.')[1];
    const priceIntStrs = priceInt.split('').reverse();

    let priceStr = '';
    priceIntStrs.forEach((item, index) => {
        const isThousand = (index + 1) % 3 === 0;
        const isLast = index + 1 === priceIntStrs.length;
        const commaStr = isThousand && !isLast ? ',' : '';
        priceStr += `${item}${commaStr}`;
    });
    const formattedPriceStr = priceStr.split('').reverse().join('');
    return `￥${isNegative ? '-' : ''}${formattedPriceStr}.${priceDecimal}`;
}

/**
 * @param {*} rate
 * @Description: 星际评分
 */
export function getRate(rate = 0) {
    return '★★★★★☆☆☆☆☆'.slice(5 - rate, 10 - rate);
}

/**
 * @param {*} time 时间戳
 * @param {*} matter 默认格式 YYYY-MM-DD HH:ii:ss
 * @Description: 格式化时间戳
 */
export function formatTime(time, matter = 'YYYY-MM-DD HH:mm:ss') {
    if (time && typeof time == "number") {
        time = time * 1000;
    } else {
        return time;
    }
    return moment(time).format(matter)
}

export function getWeek(date) { // 参数时间戳
    let week = moment(date).day()
    switch (week) {
        case 1:
            return '星期一'
        case 2:
            return '星期二'
        case 3:
            return '星期三'
        case 4:
            return '星期四'
        case 5:
            return '星期五'
        case 6:
            return '星期六'
        case 0:
            return '星期日'
    }
}



//递归
export const flatten = (arr) => {
    return [].concat(...arr.map(item => [].concat(item, ...flatten(item.children))))
}

//uuid
export function getUUID(len, radix) {
    var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('')
    var uuid = []
    var i
    radix = radix || chars.length
    if (len) {
        for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix]
    } else {
        var r
        uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'
        uuid[14] = '4'
        for (i = 0; i < 36; i++) {
            if (!uuid[i]) {
                r = 0 | Math.random() * 16
                uuid[i] = chars[(i === 19) ? (r & 0x3) | 0x8 : r]
            }
        }
    }
    return uuid.join('')
}

export function NumberToUnit(Numbers = 0) {
    if (Numbers >= 10000) {
        return (Numbers / 10000).toFixed(2) + ' 万';
    }
    return Numbers;
}
/**
 * @description: 判断元素是否滚动到底部
 * @param {*} domEvennt 滚动的dom元素对象
 * @param {*} offset 距离底部的距离触发；默认 20px
 * @return {*}
 */
export function isScrollBottom(domEvennt, offset = 20) {
    const { clientHeight, scrollTop, scrollHeight } = domEvennt;
    if ((clientHeight + scrollTop + offset) >= scrollHeight) {
        return true;
    }
    return false;
}

/**
 * @description: 设置root样式变量
 * @param {*} key 样式变量名称
 * @param {*} Value css值
 * @return {*}
 */
const setRootCss = (key, Value) => {
    document.querySelector(':root').style.setProperty(key, Value)
}
window.setRootCss = setRootCss

function deepMerge(def, newObj) {
    return merge(def, newObj);
}

function numFormat(num) {
    num = Number(num);
    if (num == 0) {
        return num + '';
    } else {
        if (num > 1 && num < 10000) {
            return num + '';
        } else {
            return (num / 10000).toFixed(2) + '万';
        }
    }
}

function Arraysort(arr = []) {
    return arr.sort((a, b) => {
        try {
            return a.sort - b.sort;
        } catch (error) {
            return false
        }
    })
}
export { setRootCss, deepMerge, numFormat, Arraysort }